//DAO  Data Access Object
//访问数据库的操作就可以使用这几个对象来进行


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//封装了针对用户表的相关操作
public class UserDao {
    //根据用户名来查询用户的详情    --登录
    //隐含约束：用户名需要唯一
    public User selectByName(String username){

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //1.连接
            connection = DBUtil.getConnection();
            //2.构造SQL
            String sql = "select * from user where username = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            //3.执行SQl
            resultSet = statement.executeQuery();
            //4.遍历结果集合
            if (resultSet.next()) {
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放对应的资源
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    //根据用户id来查询用户详情      --在获取用户信息的时候，需要用到
    public User selectById(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //1.连接
            connection = DBUtil.getConnection();
            //2.构造SQL
            String sql = "select * from user where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            //3.执行SQl
            resultSet = statement.executeQuery();
            //4.遍历结果集合
            if (resultSet.next()) {
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放对应的资源
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
}
